﻿using Iworld.Client.Framework.AuthorService;
using Iworld.ViewModel;
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Client.Framework
{
    /// <summary>
    /// 修改用户基本信息视图模型
    /// </summary>
    public class EditBaseInfoViewModel : SubPageFrameViewModelBase
    {
  
        string _username = "";
        string _qq="";
        string _twoLevelPassword="";
        string _phone = "";
        string _identityCard = "";
 
        /// <summary>
        /// 提交修改的命令
        /// </summary>
        public UniversalCommand SubmitCommand { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public string Username
        {
            get
            {
                return _username;
            }
            set
            {
                if (_username != value)
                {
                    _username = value;
                    OnPropertyChanged("Username");
                }
            }
        }

        /// <summary>
        /// 二级密码
        /// </summary>
        public string TwoLevelPassword
        {
            get
            {
                return _twoLevelPassword;
            }
            set
            {
                if (_twoLevelPassword!= value)
                {
                    _twoLevelPassword = value;
                    OnPropertyChanged("TwoLevelPassword");
                }
            }
        }

        /// <summary>
        /// QQ号码
        /// </summary>
        public string QQ
        {
            get
            {
                return _qq;
            }
            set
            {
                if (_qq != value)
                {
                    _qq = value;
                    OnPropertyChanged("QQ");
                }
            }
        }

        /// <summary>
        /// 电话号码
        /// </summary>
        public string Phone
        {
            get
            {
                return _phone;
            }
            set
            {
                if (_phone != value)
                {
                    _phone = value;
                    OnPropertyChanged("Phone");
                }
            }
        }

        /// <summary>
        /// 身份证
        /// </summary>
        public string IdentityCard
        {
            get
            {
                return _identityCard;
            }
            set
            {
                if (_identityCard != value)
                {
                    _identityCard = value;
                    OnPropertyChanged("IdentityCard");
                }
            }
        }

        public EditBaseInfoViewModel()
            : base(PageCategory.用户信息.ToString(),Page.修改基本信息.ToString())
        {
            SubmitCommand = new UniversalCommand((paramater) => {
                if (this.IsBusy) return;
                AuthorServiceClient asc = new AuthorServiceClient();
                asc.EditBaseInfoCompleted += this.WrapEventHandler<EditBaseInfoCompletedEventArgs>(EditBaseInfoCompleted);
                asc.EditBaseInfoAsync(new EditAuthorBaseInfoImport() { 
                 Phone = this.Phone,
                 QQ=this.QQ, 
                 Token = DataManager.GetValue<string>(DataKey.Iworld_Token), 
                 Username=this.Username, 
                 TwoLevelPassword=this.TwoLevelPassword
                });
                IsBusy = true; 
            });
            AuthorServiceClient serv = new AuthorServiceClient();
            serv.GetUserSelfInfoCompleted += serv_GetUserSelfInfoCompleted;
            serv.GetUserSelfInfoAsync(new GetUserSelfInfoImport() { Token = DataManager.GetValue<string>(DataKey.Iworld_Token) });
        }

        void serv_GetUserSelfInfoCompleted(object sender, GetUserSelfInfoCompletedEventArgs e)
        {
          
           if(e.Result.Success)
           {
               this.Phone = e.Result.Info.Phone;
               this.QQ = e.Result.Info.QQ;
               this.Username = e.Result.Info.Username;
           }
           else
           {
               this.ShowError(e.Result.Error);
           }
        }

        void EditBaseInfoCompleted(object sender, EditBaseInfoCompletedEventArgs e)
        {
            IsBusy = false;
          if(e.Result.Success)
          {
              this.ShowMsg("修改用户信息成功");
          }
          else
          {
              this.ShowError(e.Result.Error);
          }
        }
    }
}
